Skip to content

Conversation

@owenca
Copy link
Contributor

@owenca owenca commented Mar 26, 2025

Fix #132832

@owenca owenca added this to the LLVM 20.X Release milestone Mar 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 26, 2025

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fix #132832


Full diff: https://github.com/llvm/llvm-project/pull/133033.diff

2 Files Affected:

  • (modified) clang/lib/Format/Format.cpp (+10-3)
  • (modified) clang/unittests/Format/ConfigParseTest.cpp (+6)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index c67db4752e87b..87e36578d9958 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2129,10 +2129,17 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config,
       LanguageFound = true;
   }
   if (!LanguageFound) {
-    if (Styles.empty() || Styles[0].Language != FormatStyle::LK_None)
+    if (Styles.empty())
       return make_error_code(ParseError::Unsuitable);
-    FormatStyle DefaultStyle = Styles[0];
-    DefaultStyle.Language = Language;
+    auto DefaultStyle = Styles[0];
+    auto &DefaultLanguage = DefaultStyle.Language;
+    if (DefaultLanguage != FormatStyle::LK_None &&
+        // For backward compatibility.
+        !(DefaultLanguage == FormatStyle::LK_Cpp &&
+          Language == FormatStyle::LK_C)) {
+      return make_error_code(ParseError::Unsuitable);
+    }
+    DefaultLanguage = Language;
     StyleSet.Add(std::move(DefaultStyle));
   }
   *Style = *StyleSet.Get(Language);
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index cc42642f99252..4c17c1a50face 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -1230,6 +1230,12 @@ TEST(ConfigParseTest, ParsesConfigurationWithLanguages) {
               IndentWidth, 56u);
 }
 
+TEST(ConfigParseTest, AllowCppForC) {
+  FormatStyle Style = {};
+  Style.Language = FormatStyle::LK_C;
+  EXPECT_EQ(parseConfiguration("Language: Cpp", &Style), ParseError::Success);
+}
+
 TEST(ConfigParseTest, UsesLanguageForBasedOnStyle) {
   FormatStyle Style = {};
   Style.Language = FormatStyle::LK_JavaScript;

@github-project-automation github-project-automation bot moved this from Needs Triage to Needs Merge in LLVM Release Status Mar 26, 2025
@owenca owenca merged commit 05fb840 into llvm:main Mar 27, 2025
11 checks passed
@owenca owenca deleted the 132832 branch March 27, 2025 08:00
@github-project-automation github-project-automation bot moved this from Needs Merge to Done in LLVM Release Status Mar 27, 2025
@owenca
Copy link
Contributor Author

owenca commented Mar 27, 2025

/cherry-pick 05fb840

@llvmbot
Copy link
Member

llvmbot commented Mar 27, 2025

/pull-request #133216

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

[clang-format] Fix breaking change in 20.1.0 by automatically falling back to Cpp formatting if C formatting fails

3 participants